package com.manager.web.common;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;

import com.manager.base.BaseAction;
import com.manager.util.CodeUtil;
import com.manager.util.DateUtil;
import com.manager.util.FileUtil;
import com.opensymphony.xwork2.Action;

public class FileUploadAction extends BaseAction {
	public String fileUploadTest(){
		//文件保存地址
		String folderPath = super.getRealBasePath() + "/headImage";
		FileUtil.createFolder(folderPath);
		
		File newFile = null;
		InputStream in = null;
		OutputStream out = null;
		int bufferSize = 1024 * 2;
		StringBuffer newName = new StringBuffer();
		
		try{
			for(int i = 0; i < super.file.length; i++){
				//重新命名文件：格式为20100829122659x2e3.txt
				newName.delete(0, newName.length());
				newName.append("/");
				newName.append(DateUtil.getCurrentDateTime(false)).append("_");
				newName.append(CodeUtil.getRandomStr(4));
				newName.append(FileUtil.getSuffix(super.fileFileName[i]));
				
				newFile = new File(folderPath + newName.toString());
				
				//拷贝临时文件到批定目录
				super.copyFile(super.file[i], newFile);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		super.forward("success.jsp");
		return Action.NONE;
	}
}
